//
//  YFVisitorView.swift
//  YFSwiftWeibo
//
//  Created by tsaievan on 2017/8/25.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

class YFVisitorView: UIView {
    
    /// 访客视图的信息字典[imageName / Message]
    var visitorInfo: [String : String]? {
        
        ///< 相当于OC中的重写set方法
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            tipLabel.text = message
            if imageName == "" {
                // 只有首页才转动iconView
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imageName)
            houseIconView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    
    /// 图像视图
    fileprivate lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    /// 遮罩视图
    fileprivate lazy var maskIconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    /// 小房子
    fileprivate lazy var houseIconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    /// 提示标签
    fileprivate lazy var tipLabel: UILabel = UILabel.hm_label(withText: "关注一些任, 回这里看看有什么惊喜关注一些任, 回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.darkGray)
    
    /// 注册按钮
     lazy var registButton: UIButton? = UIButton.hm_textButton(withTitle: "注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    /// 登录按钮
     lazy var loginButton: UIButton? = UIButton.hm_textButton(withTitle: "登录", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: -界面布局
extension YFVisitorView {
    fileprivate func setupUI() {
        backgroundColor = UIColor.hm_color(withHex: 0xededed)
        tipLabel.textAlignment = .center
        
        ///< 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        if let registButton = registButton,
            let loginButton = loginButton {
            addSubview(registButton)
            addSubview(loginButton)
        }
    }
    
    fileprivate func makeConstraints() {
        // 取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 使用autoLayout
        
        ///< 1. 小圆圈
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1,
                                         constant: -60))
        
        ///< 2. 小房子
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY,
                                         multiplier: 1,
                                         constant: 0))
        
        ///< 3. 提示标签
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: 20))
        
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1,
                                         constant: 236))
        
        ///< 4. 注册按钮
        if let registButton = registButton,
            let loginButton = loginButton {
            addConstraint(NSLayoutConstraint(item: registButton,
                                             attribute: .leading,
                                             relatedBy: .equal,
                                             toItem: tipLabel,
                                             attribute: .leading,
                                             multiplier: 1,
                                             constant: 0))
            
            
            addConstraint(NSLayoutConstraint(item: registButton,
                                             attribute: .top,
                                             relatedBy: .equal,
                                             toItem: tipLabel,
                                             attribute: .bottom,
                                             multiplier: 1,
                                             constant: 40))
            
            addConstraint(NSLayoutConstraint(item: registButton,
                                             attribute: .width,
                                             relatedBy: .equal,
                                             toItem: nil,
                                             attribute: .notAnAttribute,
                                             multiplier: 1,
                                             constant: 100))
            
            addConstraint(NSLayoutConstraint(item: loginButton,
                                             attribute: .trailing,
                                             relatedBy: .equal,
                                             toItem: tipLabel,
                                             attribute: .trailing,
                                             multiplier: 1,
                                             constant: 0))
            
            
            addConstraint(NSLayoutConstraint(item: loginButton,
                                             attribute: .top,
                                             relatedBy: .equal,
                                             toItem: tipLabel,
                                             attribute: .bottom,
                                             multiplier: 1,
                                             constant: 40))
            
            addConstraint(NSLayoutConstraint(item: loginButton,
                                             attribute: .width,
                                             relatedBy: .equal,
                                             toItem: registButton,
                                             attribute: .width,
                                             multiplier: 1,
                                             constant: 0))
            
            ///< 利用VFL进行布局
            let metrics = ["spacing" : -20];
            let views = ["maskIconView" : maskIconView, "registButton" : registButton] as [String : Any]
            
            addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: ["maskIconView" : maskIconView]))
            
            addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-(spacing)-[registButton]", options: [], metrics: metrics, views: views))
        }
    }
    
    fileprivate func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * Double.pi
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        
        ///< 设置这个属性的目的是当动画完成之后不移除
        animation.isRemovedOnCompletion = false
        iconView.layer.add(animation, forKey: nil)
    }
}



